package com.dang.alex.dao.imp;

import com.dang.alex.dao.UserDAO;
import com.dang.alex.model.User;
import com.dang.alex.util.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDAOImp implements UserDAO{
	
	String saveSql="INSERT INTO d_user(email,nickname,password,user_integral,is_email_verify,email_verify_code,last_login_time,last_login_ip) VALUEs(?,?,?,?,?,?,?,?)";
	String findIdSql="select * from d_user where id=?";
	String findEmailSql="select * from d_user where email=?";
	String upDateSql="update d_user set email=?,nickname=?,password=?,user_integral=?,is_email_verify=?,email_verify_code=?,last_login_time=?,last_login_ip=? where id=?";
	String translate_sql="select * from d_user where email=? and password=?";
	public void save(User u) throws Exception {
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs= null;
		try {
			conn=DBUtils.getTnstace().getConnection();
			ps=conn.prepareStatement(saveSql);
			ps.setString(1, u.getEmail());
			ps.setString(2, u.getNickname());
			ps.setString(3, u.getPassword());
			ps.setInt(4,u.getUserIntegral());
			if( u.isEmailVerify()) {
				ps.setString(5, "T");
			}else {
				ps.setString(5, "F");
			}
			ps.setString(6, u.getEmailVerifyCode());
			ps.setLong(7, u.getLastLoginTime());
			ps.setString(8, u.getLastLoginIp());
			ps.executeUpdate();
			rs=ps.getGeneratedKeys();
			rs.next();
			int id =rs.getInt(1);
			u.setId(id);
		} catch (Exception e) {
			
			e.printStackTrace();
		}finally {
			DBUtils.getTnstace().close(conn);
			DBUtils.getTnstace().close(ps);
			DBUtils.getTnstace().close(rs);
		}
		
	}
	
	public User findById(int id) {
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs= null;
		User user= null;
		try {
			conn=DBUtils.getTnstace().getConnection();
			ps=conn.prepareStatement(findIdSql);
			ps.setInt(1, id);
			rs=ps.executeQuery();
			user=new User();
			if(rs.next()) {
				user.setEmail(rs.getString("email"));
				user.setNickname(rs.getString("nickname"));;
				user.setPassword(rs.getString("password"));
				user.setUserIntegral(rs.getInt("user_integral"));
				if(rs.getString("is_email_verify")=="T") {
					user.setEmailVerify(true);
				}else {
					user.setEmailVerify(false);
				}
				user.setEmailVerifyCode(rs.getString("email_verify_code"));
				user.setLastLoginTime(rs.getLong("last_login_time"));
				user.setLastLoginIp(rs.getString("last_login_ip"));
			}
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}finally {
			DBUtils.getTnstace().close(conn);
			DBUtils.getTnstace().close(ps);
			DBUtils.getTnstace().close(rs);
		}
		return user;
	}

	
	public User findByEmail(String email) throws Exception {
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs= null;
		User user= null;
		try {
			conn=DBUtils.getTnstace().getConnection();
			ps=conn.prepareStatement(findEmailSql);
			ps.setString(1, email);
			rs=ps.executeQuery();
			if(rs.next()) {
				user=new User();
				user.setId(rs.getInt("id"));
				user.setEmail(rs.getString("email"));
				user.setNickname(rs.getString("nickname"));
				user.setPassword(rs.getString("password"));
				user.setUserIntegral(rs.getInt("user_integral"));
				if(rs.getString("is_email_verify").equals("T")) {
					user.setEmailVerify(true);
				}else {
					user.setEmailVerify(false);
				}
				user.setEmailVerifyCode(rs.getString("email_verify_code"));
				user.setLastLoginTime(rs.getLong("last_login_time"));
				user.setLastLoginIp(rs.getString("last_login_ip"));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBUtils.getTnstace().close(conn);
			DBUtils.getTnstace().close(ps);
			DBUtils.getTnstace().close(rs);
		}
		
		return user;
	}

	
	public void upDate(User u) throws Exception {
		Connection conn=null;
		PreparedStatement ps=null;
		try {
			conn=DBUtils.getTnstace().getConnection();
			ps=conn.prepareStatement(upDateSql);
			ps.setString(1, u.getEmail());
			ps.setString(2, u.getNickname());
			ps.setString(3, u.getPassword());
			ps.setInt(4,u.getUserIntegral());
			if( u.isEmailVerify()) {
				ps.setString(5, "T");
			}else {
				ps.setString(5, "F");
			}
			ps.setString(6, u.getEmailVerifyCode());
			ps.setLong(7, u.getLastLoginTime());
			ps.setString(8, u.getLastLoginIp());
			ps.setInt(9, u.getId());
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBUtils.getTnstace().close(conn);
			DBUtils.getTnstace().close(ps);
		}
	}

	@Override
	public User translate(String email, String password) throws Exception {
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs= null;
		User user= null;
		try {
			conn=DBUtils.getTnstace().getConnection();
			ps=conn.prepareStatement(translate_sql);
			ps.setString(1, email);
			ps.setString(2, password);
			rs=ps.executeQuery();
			
			if(rs.next()) {
				user= new User();
				user.setId(rs.getInt("id"));
				user.setEmail(rs.getString("email"));
				user.setNickname(rs.getString("nickname"));;
				user.setPassword(rs.getString("password"));
				user.setUserIntegral(rs.getInt("user_integral"));
				if(rs.getString("is_email_verify")=="T") {
					user.setEmailVerify(true);
				}else {
					user.setEmailVerify(false);
				}
				user.setEmailVerifyCode(rs.getString("email_verify_code"));
				user.setLastLoginTime(rs.getLong("last_login_time"));
				user.setLastLoginIp(rs.getString("last_login_ip"));
				
			}
			
	} catch (Exception e) {
		// TODO: handle exception
	}finally {
		DBUtils.getTnstace().close(conn);
		DBUtils.getTnstace().close(ps);
		DBUtils.getTnstace().close(rs);
	}
	return user;
		
	}

}
